<!--校车管理页面-->
<template>
  <!--  新建校车弹窗-->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;" :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车牌号">
            <el-input placeholder="请输入内容" v-model="saveVirtualForm.licensePlate"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="校车类型">
            <el-select placeholder="请选择校车类型" v-model="saveVirtualForm.type">
              <el-option v-for="item in virtualTypeArr"
                         :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="容量">
            <el-input placeholder="请输入内容" v-model="saveVirtualForm.capacity"></el-input>
          </el-form-item>
        </el-col>

      </el-row>

    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveVirtual">确定</el-button>
    </template>
  </el-dialog>
  <!--  搜索校车 -->
  <el-card style="margin: 20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="车牌号" style="width:290px">
        <el-input placeholder="请输入车牌号" style="width:220px;"
                  v-model="searchVirtualForm.licensePlate" @keydown.enter="loadVirtual"></el-input>
      </el-form-item>
      <el-form-item label="容量" style="width:290px">
        <el-input placeholder="请输入容量" style="width:220px;"
                  v-model="searchVirtualForm.capacity" @keydown.enter="loadVirtual"></el-input>
      </el-form-item>
      <el-form-item label="校车状态" style="width:290px">
        <el-select placeholder="请选择" v-model="searchVirtualForm.status" @keydown.enter="loadVirtual">
          <el-option v-for="item in appOptions"
                     :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadVirtual">查询</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!--  校车列表展示-->
  <el-card style="margin: 20px;">
    <el-button type="primary" @click="virtualAssets">新增</el-button>
    <el-button type="danger"  @click="batchDelete">批量删除</el-button>
    <el-button type="success">导出</el-button>
    <el-table style="margin-top: 20px" :data="virtualArr" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column type="index" label="序号" width="80" align="center"/>
      <el-table-column prop="licensePlate" label="车牌号码" align="center"/>
      <el-table-column prop="type" label="校车类型" align="center"
                       :formatter="virtualTypeFormatter"/>
      <el-table-column prop="capacity" label="容量" align="center"/>
      <el-table-column label="校车状态" prop="status" align="center">
        <template #default="scope">
          <el-switch
              v-model="scope.row.status"
              active-value="1"
              inactive-value="0"
              @change="updateStatus(scope.row.id, scope.row.status)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editVirtual(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteVirtual(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage, ElMessageBox} from "element-plus";
import index from "vuex";
import selectedRows from "core-js/internals/array-iteration";

const dialogTitle = ref('新增校车');
const dialogVisible = ref(false);
//定义处理弹窗关闭的方法
const handleClose = ()=>{
  if(confirm('您确认要关闭弹窗吗?')){
    saveVirtualForm.value={};
    dialogVisible.value = false;
  }
}

//定义对象用来保存虚拟资产数据
const saveVirtualForm = ref({
  type:'',
  licensePlate:'',
  capacity:'',
  status:'',
});

//点击新增按钮时调用
const  virtualAssets= ()=>{
  dialogTitle.value = '新增校车';
  dialogVisible.value = true;
}
//定义保存虚拟资产的方法
const saveVirtual = ()=>{
  let data = qs.stringify(saveVirtualForm.value);
  axios.post(BASE_URL+'/v1/bus/save',data).then((response)=>{
    if (response.data.code===2000){
      ElMessage.success("保存成功!");
      saveVirtualForm.value={};
      dialogVisible.value=false;
      loadVirtual();
    }else {
      ElMessage.error(response.data.msg)
    }
  })
}
//定义对象保存虚拟财产的搜索条件
const searchVirtualForm = ref({
  status:'',
  licensePlate:'',
  capacity:'',
})

const virtualArr = ref([]);

const loadVirtual = ()=>{
  let data = qs.stringify(searchVirtualForm.value);
  axios.get(BASE_URL+'/v1/bus/select?'+data).then((response)=>{
    if (response.data.code === 2000){
      virtualArr.value = response.data.data;
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
//自动刷新加载虚拟财产数据
onMounted(()=>{
  loadVirtual();
})

//定义重置搜索的方法
const resetSearch = ()=>{
  searchVirtualForm.value = {};
  loadVirtual();
}
//定义编辑资产的方法
const editVirtual = (id) =>{
  dialogVisible.value = true;
  dialogTitle.value = '编辑校车'
  axios.get(BASE_URL+'/v1/bus/select?id='+id).then((response)=>{
    if (response.data.code===2000){
      saveVirtualForm.value = response.data.data[0];
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
//定义删除资产的方法
const deleteVirtual = (id) =>{
  if (confirm('您确定要删除本校车吗?')){
    axios.post(BASE_URL+'/v1/bus/delete/'+id).then((response)=>{
      if (response.data.code === 2000){
        ElMessage.success('删除成功!');
        loadVirtual();
      }else {
        ElMessage.error(response.data.msg);
      }
    })
  }
}

//定义数组用来保存虚拟资产字典项
const virtualTypeArr = ref([]);
onMounted(()=>{
  loadDictOptions(virtualTypeArr,'busType');
})

const virtualTypeFormatter = (row,column,cellValue,index)=>{
  if(!virtualTypeArr.value){
    return cellValue;
  }
  for(let item of virtualTypeArr.value){
    if(item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}

const appOptions = ref([]);
onMounted(()=>{
  loadDictOptions(appOptions,'busStatus')
})




//定义批量删除的数组
const selectedIdsValue = ref([]);
// 监听选择变化
const handleSelectionChange = (selection) => {
  // 更新当前选中的行
  selectedRows.value = selection;
  // 提取所有选中行的ID
  selectedIdsValue.value = selection.map(row => row.id);
};


// 批量删除的方法
const batchDelete = () => {
  if (selectedIdsValue.value.length === 0) {
    Info('请选择要删除的行');
    return;
  }

  let data = selectedIdsValue.value.join(',');
  ElMessageBox.confirm('确定删除选中的校车吗？', '提示', {
    customClass: 'custom-message-box',
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    axios.post(BASE_URL + '/v1/bus/delete/batch/' + data)
        .then((response) => {
          if (response.data.code === 2000) {
            OKInfo('删除成功!')
            loadVirtual();
          } else {
            ElMessage({
              message: response.data.msg,
              type: 'error',
              showClose: true,
              center: true,
            })
          }
        })
  }).catch(() => {
    Info('已取消删除')
  });

};

const updateStatus = (id, status) => {
  axios.put(BASE_URL + '/v1/bus/update/status/' + id + '/' + status)
      .then((response) => {
        if (response.data.code === 2000) {
          OKInfo('状态修改成功!')
        } else {
          ElMessage({
            message: response.data.msg,
            type: 'error',
            showClose: true,
            center: true,
          })
        }
      })
}

</script>

<style scoped>

</style>